import { ApiProperty, getSchemaPath } from "@nestjs/swagger";
import { CrudValidationGroups } from "@nestjsx/crud";
import { IsNumber, IsOptional, IsString, ValidateNested, ValidateIf, IsNotEmpty } from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn, BeforeInsert, BeforeUpdate, getRepository } from "typeorm";
import { file } from "./about/file";
import { BaseEntity } from "./base.entity";
import { FundsTypeEmum } from "./enum/funds-type.enum";
import { Group } from "./group.entity";
import { Village } from "./village.entity";
import { hashSync } from "bcryptjs";
import { Town } from "./town.entity";

const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class Funds extends BaseEntity {
  @ApiProperty({
    description: '资金主键',
    required: false
  })
  @PrimaryGeneratedColumn()
  id ?: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => Village)
      }
    ]
  })
  @ManyToOne(() => Village, Village => Village.funds, {
    eager: true
  })
  village: Number | Village

  @ApiProperty({
    example: 1,
    description: '所属组',
    required: true,
  })
  @ManyToOne(
    () => Group,
    Group => Group.funds,
    {
      eager: true,
    },
  )
  group: Number | Group;
  
  
  @ApiProperty({
    example: 0,
    type: IsNumber,
    description: '资金类型',
    required: true,
    enum: FundsTypeEmum
  })
  @IsNumber({}, { always: true })
  @Column({
    type: "enum",
    enum: FundsTypeEmum
  })
  type: FundsTypeEmum

  @ApiProperty({
    example: '2010-10-10',
    description: '资金时间',
    required: true
  })
  @IsString()
  @Column({
    type: "char",
    length: 255,
  })
  time: string

  @ApiProperty({
    example: 1,
    description: '资金收入',
    required: true
  })
  // @IsDecimal({}, { always: true })
  // @IsNumber()
  @Column({
    type: 'decimal',
    precision: 14,
    scale: 2,
    zerofill: false
  })
  input: number

  @ApiProperty({
    example: 1,
    description: '资金支出',
    required: true
  })
  // @IsDecimal({}, { always: true })
  // @IsNumber()
  @Column({
    type: 'decimal',
    precision: 14,
    scale: 2,
    zerofill: false
  })
  output: number

  @ApiProperty({
    example: '资金事由',
    description: '资金事由',
    required: true
  })
  @IsString({ always: true })
  @Column({
    type: "text",
  })
  cause: string

  @ApiProperty({
    example: '备注信息',
    description: '备注信息',
    required: true
  })
  @IsOptional({ always: true })
  @IsString({ always: true })
  @Column({
    type: "text",
    nullable: true,
  })
  remarks: string


  @ApiProperty({
    example: '资金详情 -> 相关文件',
    description: '资金详情 -> 相关文件',
    required: true
  })
  @ValidateNested()
  @IsOptional({ always: true })
  @Column({
    type: "simple-json",
    nullable: true,
  })
  relatedDocuments: file []

  @ApiProperty({
    example: '月总结名字自动生成 -详细可省略此字段',
    description: '月总结名字自动生成',
  })
  @IsString()
  @IsOptional()
  @Column({
    type: "char",
    length: 255,
    nullable: true
  })
  name: string

  @ApiProperty({
    example: 1,
    description: '月总结下的 详细信息',
  })
  @OneToMany(() => Funds, Funds => Funds.fatherFund)
  sonFunds: Funds []

  @ApiProperty({
    example: 1,
    description: '详细信息 所属 月总结信息',
  })
  @ManyToOne(() => Funds, Funds => Funds.sonFunds)
  fatherFund: Funds

  @BeforeInsert()
  @BeforeUpdate()
  async generateName() {
    const villageRepository = getRepository(Village);
    const townRepository = getRepository(Town);
    let villageId = this.village as number
    const village = await villageRepository
                            .findOne(villageId)
    const town = await townRepository
                            .findOne(village.town as number)
    let timeAtt = this.time.split('-')                      
    this.name = `${town.name}${village.name}${timeAtt.shift()}年${timeAtt.shift()}月${this.type == 0 ? '现今收支' : '银行收支'}明细`
    console.log(this.name)
  }
}